import { defineStore } from "pinia";

export const stateStore = defineStore({
  id: "state", //命名
  state: () => {
    return {
      name: "菠萝",
      age: 99,
    };
  },
  //   数据持久化
  persist: {
    token: "hhhhh",
    strategies: [
      {
        storage: sessionStorage,
        path: ["name"],
      },
    ],
  },
  getters: {
    getterName(state) {
      console.log("使用了getter");
      return "我是什么水果？我是" + state.name;
    },
  },
  actions: {
    //还支持调用别的actions
    updateName(name, age) {
      this.name = name;
      this.updateAge(age);
    },
    updateAge(age) {
      this.age = age;
    },
    //action 可以像写一个简单的函数一样支持 async/await的语法
    async asyncfun() {
      const timer = setTimeout(() => {
        this.name = "桃子";
      }, 1000);
      await timer;
    },
  },
});
